package com.bawei.caohao20240805.view.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import coil.load
import com.ayvytr.adapter.smart
import com.bawei.caohao20240805.MyApp
import com.bawei.caohao20240805.R
import com.bawei.caohao20240805.base.BaseActivity
import com.bawei.caohao20240805.databinding.ActivityMainBinding
import com.bawei.caohao20240805.view.fragment.LoginFragment
import com.bawei.caohao20240805.view.fragment.MyFragment
import com.bawei.caohao20240805.view.fragment.TimeFragment
import com.bawei.caohao20240805.viewmodel.WelViewModel
import com.bawei.comm.model.bean.LeiBean
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener

class MainActivity : BaseActivity<WelViewModel,ActivityMainBinding>(R.layout.activity_main) {

    override fun dataObSer() {
        mViewModel.bandata.observe(this, Observer {
            if(it.status==2){
                AlertDialog.Builder(this)
                    .setTitle("版本更新")
                    .setMessage("1.有新版本\n2.有片看\n3.页面更新\n4.功能升级\n")
                    .setNegativeButton("取消",null)
                    .setPositiveButton("更新",{ittt,eewrw->
                        edit.putString("id","206352")
                        edit.commit()
                        Toast.makeText(this, "正在升级", Toast.LENGTH_SHORT).show()
                    })
                    .show()
            }else{
                Toast.makeText(this, "已是最新版本", Toast.LENGTH_SHORT).show()
            }

        })
        mViewModel.shidata.observe(this, Observer {
            if(it.result.status){
                Toast.makeText(this, "已认证", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this, "还未实名认证", Toast.LENGTH_SHORT).show()
                startActivity(Intent(this,ShiActivity::class.java))
            }
        })
    }
    var edit = MyApp.sh.edit()
    override fun initView() {
        val title= arrayOf("首页","广场","消息","我的")
        val fragment= arrayOf(LoginFragment(),TimeFragment(),MyFragment(),MyFragment())
        mDing.page.offscreenPageLimit=3
        mDing.page.adapter=object :FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {
                return fragment.size
            }

            override fun getItem(position: Int): Fragment {
                return fragment[position]
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return title[position]
            }

        }
        mDing.tab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if(tab?.position==1){
                    mViewModel.getshi(MyApp.userId,MyApp.sessionId)
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        mDing.tab.setupWithViewPager(mDing.page)
        var string = MyApp.sh.getString("id","0")
        if(string.equals("0")){
            mViewModel.getban("106352")
        }else{
            if (string != null) {
                mViewModel.getban(string)
            }
        }

    }

}